﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RC1.admin
{
    public partial class edit_user : System.Web.UI.Page
    {
        int user_id;
        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = Convert.ToInt32(Request.QueryString["id"]);
            errorLabel.Visible = false;
            if (!Page.IsPostBack)
            {
                populateBoxes();
            }
        }

        private void populateBoxes()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var user = (from u in db.lpusers
                        join ui in db.user_info on u.user_id equals ui.user_id
                        where u.user_id == user_id
                        select new
                        {
                            id = u.user_id,
                            email = u.user_email,
                            password = u.user_password,
                            first_name = ui.user_info_first_name,
                            last_name = ui.user_info_last_name,
                            phone = ui.user_info_phone_number
                        }).SingleOrDefault();

            emailTextBox.Text = user.email;
            passwordTextBox.Text = user.password;
            firstnameTextBox.Text = user.first_name;
            lastnameTextBox.Text = user.last_name;
            phoneNumberTextBox.Text = user.phone.ToString();
        }

        private void updateInfo()
        {
            String email = emailTextBox.Text;
            String password = passwordTextBox.Text;
            String fn = firstnameTextBox.Text;
            String ln = lastnameTextBox.Text;
            String p = phoneNumberTextBox.Text;
            int phone = -1;
            if (p.Length > 0)
            {
                phone = Convert.ToInt32(p);
            }

            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var preUser = (from u in db.lpusers
                           where u.user_email == email
                           select u).SingleOrDefault();
            if (preUser == null)
            {
                var user = (from u in db.lpusers
                            where u.user_id == user_id
                            select u).SingleOrDefault();

                user.user_email = email;
                user.user_password = password;

                var info = (from ui in db.user_info
                            where ui.user_id == user_id
                            select ui).SingleOrDefault();
                info.user_info_first_name = fn;
                info.user_info_last_name = ln;
                if (phone != -1)
                {
                    info.user_info_phone_number = phone;
                }
                db.SaveChanges();
                goBack();
            }
            else
            {
                printError("T-postur longu skrásettur");
            }
        }

        private void goBack()
        {
            if (Request.QueryString["p"] != null)
            {
                if (Request.QueryString["p"].Equals("t"))
                {
                    Response.Redirect("teacher.aspx");
                }
                else
                {
                    Response.Redirect("student.aspx");
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            updateInfo();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            goBack();
        }

        private void printError(String s)
        {
            errorLabel.Text = "<tr><td colspan='2' align='center'><span style='color:red;'>" + s + "</span></td></tr>";
            errorLabel.Visible = true;
        }
    }
}